package com.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dto.UserInfo;
import com.service.UpdatePwdService;


/**
 * @时间：2018/4/17
 * @author 柴希金
 * @功能：忘记密码
 */
@Controller
public class UpdatePwdAction {
	
	@Autowired
	private UpdatePwdService ups;
	
	
		
		//发送短信验证
		@RequestMapping("/note.do")
		@ResponseBody
		public Map<String,Object> getUserByPhone(String phone){
			Map<String,Object> map=new HashMap<String,Object>();
			UserInfo user=ups.getUserByPhone(phone);
			if(user!=null) {
				map.put("result", true);
			}else {
				map.put("result", false);
			}
			return map;
		}
		
		//判断输入的验证码是否正确
		@RequestMapping("/forget.do")
		@ResponseBody
		public Map<String,Object> getPhoneByCodeAndPhone(String phone,String code,HttpSession session) {
			Map<String,String> map1=new HashMap<String,String>();
			Map<String,Object> map2=new HashMap<String,Object>();
			map1.put("phone", phone);
			map1.put("code",code);
			String s=ups.getPhoneByCodeAndPhone(map1);
			if(s!=null){
				session.setAttribute("phone", s);
				map2.put("result", true);
			}else{
				map2.put("result", false);
			}
			return map2;
		}
		
		//根据电话号码修改密码
		@RequestMapping("/updatePassword.do")
		@ResponseBody
		public Map<String,Object> updatePassword(String newPassword,String tooPassword,HttpSession session) {
			String phone=(String) session.getAttribute("phone");
			Map<String,Object> map1=new HashMap<String,Object>();
			Map<String,String> map2=new HashMap<String,String>();
			if(newPassword.equals(tooPassword)) {
				map2.put("newPassword", newPassword);
				map2.put("phone", phone);
				ups.updatePassword(map2);
				map1.put("result", true);
			}else {
				map1.put("result", false);
			}
			return map1;
			
		}
}
